package com.yxp.easy;

public class Problem1047 {
    public static void main(String[] args) {
        Problem1047 problem1047 = new Problem1047();
        String s = "abbaca";
        System.out.println(problem1047.remove(s));


    }

    public String remove(String S){
        StringBuffer stack = new StringBuffer();
        int top = -1;
        for (int i = 0; i < S.length(); ++i) {
            char ch = S.charAt(i);
            if (top >= 0 && stack.charAt(top) == ch) {
                stack.deleteCharAt(top);
                --top;
            } else {
                stack.append(ch);
                ++top;
            }
        }
        return stack.toString();
    }
}
